package com.bawei.a2010douyin.im;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.bawei.a2010douyin.R;
import com.hyphenate.EMCallBack;
import com.hyphenate.EMChatRoomChangeListener;
import com.hyphenate.EMMessageListener;
import com.hyphenate.EMValueCallBack;
import com.hyphenate.chat.EMChatRoom;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMCustomMessageBody;
import com.hyphenate.chat.EMGroupReadAck;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMMessageReactionChange;
import com.hyphenate.exceptions.HyphenateException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 1。登陆
 * 2。注册
 * 3。加入聊天室 退出聊天室
 * 4。监听聊天室：xxx进来了 xxx退出了
 * 5。发送消息
 * 6。接受消息
 *
 */
public class HuanXinActivity extends AppCompatActivity {
    private EditText etUsername;
    private EditText etPassword;
    private EditText etMessage;
    private TextView tv;
    private String chatRoomId = "211150599946242";
    //监听器：聊天室
    private EMChatRoomChangeListener  chatRoomChangeListener = new EMChatRoomChangeListener() {
        @Override
        public void onChatRoomDestroyed(String roomId, String roomName) {

        }

        @Override
        public void onMemberJoined(String roomId, String participant) {//有人主动进入
            //切换到主线程更新Ui
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tv.setText(participant+"进来了，张又因在这给您编年了");
                }
            });

        }

        @Override
        public void onMemberExited(String roomId, String roomName, String participant) {//有人主动退出
            //切换到主线程更新Ui
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tv.setText(participant+"退出了，张又因不开心了嘤嘤嘤");
                }
            });

        }

        @Override
        public void onRemovedFromChatRoom(int reason, String roomId, String roomName, String participant) {

        }

        @Override
        public void onMuteListAdded(String chatRoomId, List<String> mutes, long expireTime) {

        }

        @Override
        public void onMuteListRemoved(String chatRoomId, List<String> mutes) {

        }

        @Override
        public void onWhiteListAdded(String chatRoomId, List<String> whitelist) {

        }

        @Override
        public void onWhiteListRemoved(String chatRoomId, List<String> whitelist) {

        }

        @Override
        public void onAllMemberMuteStateChanged(String chatRoomId, boolean isMuted) {

        }

        @Override
        public void onAdminAdded(String chatRoomId, String admin) {

        }

        @Override
        public void onAdminRemoved(String chatRoomId, String admin) {

        }

        @Override
        public void onOwnerChanged(String chatRoomId, String newOwner, String oldOwner) {

        }

        @Override
        public void onAnnouncementChanged(String chatRoomId, String announcement) {

        }
    };
    //监听器：消息
    private EMMessageListener msgListener = new EMMessageListener() {
        @Override
        public void onMessageReceived(List<EMMessage> messages) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    for (EMMessage message : messages) {
                        //1:消息来自谁
                        String from = message.getFrom();
                        //2：消息类型:
                        EMCustomMessageBody emCustomMessageBody = (EMCustomMessageBody) message.getBody(); //自定义消息体
                        String event = emCustomMessageBody.event();//消息类型
                        Map<String, String> params = emCustomMessageBody.getParams();//消息内容
                        if("gift".equals(event)){//礼物消息
                            tv.setText(from+"发送了一个礼物："+params.get("name")+"价值"+params.get("money"));

                        }else if("text".equals(event)){//文本消息
                            tv.setText(from+"："+params.get("msg1"));
                        }


                    }
                }
            });
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_huan_xin);
        etUsername = (EditText) findViewById(R.id.et_username);
        etPassword = (EditText) findViewById(R.id.et_password);
        etMessage = (EditText) findViewById(R.id.et_message);
        tv = (TextView) findViewById(R.id.tv);
    }
    //登陆
    public void login(View view) {
        IMUtils.getInstance().login(etUsername.getText().toString(), etPassword.getText().toString(), new IMCallBack<String, String>() {
            @Override
            public void onOk(String s) {
                Toast.makeText(HuanXinActivity.this, ""+s, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(String s) {
                Toast.makeText(HuanXinActivity.this, ""+s, Toast.LENGTH_SHORT).show();
            }
        });

    }
    //注册
    public void register(View view) {
        IMUtils.getInstance().register(etUsername.getText().toString(), etPassword.getText().toString(), new IMCallBack<String, String>() {
            @Override
            public void onOk(String s) {
                Toast.makeText(HuanXinActivity.this, ""+s, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(String s) {
                Toast.makeText(HuanXinActivity.this, ""+s, Toast.LENGTH_SHORT).show();

            }
        });
    }
    //加入聊天室：
    public void join(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 加入聊天室
                EMClient.getInstance().chatroomManager().joinChatRoom(chatRoomId, new EMValueCallBack<EMChatRoom>() {
                    @Override
                    public void onSuccess(EMChatRoom value) {
                        Log.d("ytx", "加入成功: ");
                        // 注册聊天室回调
                        EMClient.getInstance().chatroomManager().addChatRoomChangeListener(chatRoomChangeListener);
                        // 注册消息监听
                        EMClient.getInstance().chatManager().addMessageListener(msgListener);
                    }

                    @Override
                    public void onError(int error, String errorMsg) {

                    }
                });
            }
        }).start();
    }
    //退出聊天室
    public void exit(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                EMClient.getInstance().chatroomManager().leaveChatRoom(chatRoomId);
                Log.d("ytx", "退出聊天室: ");
                // 移除聊天室回调
                EMClient.getInstance().chatroomManager().removeChatRoomListener(chatRoomChangeListener);
                // 解注册消息监听
                EMClient.getInstance().chatManager().removeMessageListener(msgListener);
            }
        }).start();
    }
    //发送普通文本消息
    public void sendMessage(View view) {
        HashMap<String, String> map = new HashMap<>();
        map.put("msg1",etMessage.getText().toString());
        map.put("msg2",etMessage.getText().toString());
        IMUtils.getInstance().sendMessage("text",map,chatRoomId);
    }
    //发送礼物消息
    public void sendGift(View view) {
        HashMap<String, String> map = new HashMap<>();
        map.put("name","嘉年华");
        map.put("money","2000");
        IMUtils.getInstance().sendMessage("gift",map,chatRoomId);

    }
}